<template>
  <div>
    <!-- 按钮 -->
    <el-button style="margin:10px 10px" type="primary" icon="el-icon-refresh-right" circle
      @click="getPenaltyConfigurationList"></el-button>
    <el-button type="primary" icon="el-icon-plus" style="margin:10px 0" @click="getShow">添加</el-button>
    <!-- 表格 -->
    <el-table style="width: 100%;" border :data="list">
      <el-table-column prop="id" label="id" width="80" align="center">
      </el-table-column>
      <el-table-column prop="name" label="处罚名称" width="200" align="center">
      </el-table-column>
      <el-table-column label="处罚类型" align="center" width="200">
        <template slot-scope="{ row, $index }">
          <p v-if="row.type == 1">禁言</p>
          <p v-else-if="row.type == 2">封号</p>
          <p v-else>删除对应信息</p>
        </template>
      </el-table-column>
      <el-table-column prop="dayTime" label="处罚时长" align="center">
      </el-table-column>
      <el-table-column prop="intro" label="处罚描述" align="center">
      </el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row, $index }">
          <!-- <el-button type="primary" icon="el-icon-edit" circle></el-button> -->
          <el-tooltip content="修改处罚" placement="top" slot="reference">
            <el-button type="primary" icon="el-icon-edit" @click="goUpdate(row)"></el-button>
          </el-tooltip>
          <el-tooltip content="删除处罚" placement="top" slot="reference">
            <el-button type="danger" icon="el-icon-delete" @click="DeletePenaltyConfiguration(row.id)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <!-- 对话框  -->
    <el-dialog :title="state ? '添加处罚' : '修改处罚'" :visible.sync="dialogFormVisible">
      <el-form style="width:80%" :model="tmFrom" :rules="rules" ref="ruleForm">
        <el-form-item label="处罚名称" label-width="100px" prop="name">
          <el-input autocomplete="off" v-model="tmFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="处罚类型" label-width="100px" prop="type">
          <el-radio-group v-model="tmFrom.type">
            <el-radio :label="1">禁言</el-radio>
            <el-radio :label="2">封号</el-radio>
            <el-radio :label="3">删除对应信息</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处罚时长/天" label-width="100px">
          <el-input autocomplete="off" v-model="tmFrom.dayTime" placeholder="以天为单位，禁言需填"></el-input>
        </el-form-item>
        <el-form-item label="处罚描述" label-width="100px" prop="intro">
          <el-input autocomplete="off" v-model="tmFrom.intro"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="AddPenaltyConfiguration" v-if="state">确 定</el-button>
        <el-button type="primary" @click="UpdatePenaltyConfiguration" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
export default {
  name: 'Bannar',
  data() {
    return {
      // 对话框显示与隐藏控制
      dialogFormVisible: false,
      // 表单对象
      tmFrom: {
        name: '',
        type: '',
        dayTime: '',
        intro: ''
      },
      //列表数据
      list: [],
      // 当前为修改还是添加 修改为false 添加为true
      state: true,
      // 表单验证
      rules: {
        name: [
          { required: true, message: '请输入处罚名称', trigger: 'blur' },
        ],
        type: [
          { required: true, message: '请选择处罚类型', trigger: 'blur' }
        ],
        intro: [
          { required: true, message: '请输入处罚描述', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getPenaltyConfigurationList()
  },
  computed: {
    headers() {
      return {
        'session_token': getToken()
      };
    }
  },
  methods: {
    // 所有违规
    async getPenaltyConfigurationList() {
      let res = await this.$API.report.getPenaltyConfigurationList()
      this.list = res.data
    },
    // 上传图片
    handleAvatarSuccess(res, file) {
      this.tmFrom.picSrc = res.data.imgageUrl;
    },
    // 取消
    getShow() {
      this.state = true
      this.dialogFormVisible = true
      this.tmFrom = {
        name: '',
        type: '',
        dayTime: '',
        intro: ''
      }
    },
    // 添加
    AddPenaltyConfiguration() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.report.AddPenaltyConfiguration(this.tmFrom);
          this.dialogFormVisible = false;
          this.getPenaltyConfigurationList();
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    // 点击修改
    goUpdate(row) {
      this.state = false
      this.dialogFormVisible = true;
      // 浅拷贝  让tmFrom收到的数据非服务器直接传回的数据
      this.tmFrom = { ...row };
    },
    // 点击修改
    UpdatePenaltyConfiguration() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.report.UpdatePenaltyConfiguration(this.tmFrom);
          this.dialogFormVisible = false;
          this.getPenaltyConfigurationList();
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    // 删除
    async DeletePenaltyConfiguration(id) {
      let res = await this.$API.report.DeletePenaltyConfiguration({ id: id });
      this.getPenaltyConfigurationList();
    }
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>